﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModelProject.Entities;
using System.Globalization;
using ModelProject.Repositorios;
using ModelProject;
using DataProject.UnitOfWork;
using System.Data.Objects;
using ModelProject.DTOs;

namespace DataProject.Repositorios
{
    public class ClientesRepository : Repository<Cliente>, IClientesRepository
    {
        public ClientesRepository(IQueryableUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        public IEnumerable<ClienteDTO> GetClientesDTO()
        {
            var activeContext = UnitOfWork as IMainModuleUnitOfWork;
            if (activeContext == null)
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Resources.Messages.InvalidContext", this.GetType().Name));

           
            var result = (from c in activeContext.Cliente
                          select new ClienteDTO()
                          {
                              ClienteId = c.ClienteId,
                              Apellido = c.Apellido,
                              Nombre = c.Nombre,
                              NroDocumento = c.NroDocumento,
                              TipoDocumentoDescripcion = c.TipoDocumento.Descripcion,
                              TipoDocumentoId = c.TipoDocumentoId
                          });
            return result;
        }

        public ClienteDTO GetClienteDTOById(int clienteId)
        {
            var activeContext = UnitOfWork as IMainModuleUnitOfWork;
            if (activeContext == null)
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Resources.Messages.InvalidContext", this.GetType().Name));

            var result = (from c in activeContext.Cliente
                          where c.ClienteId == clienteId
                          select new ClienteDTO()
                          {
                              ClienteId = c.ClienteId,
                              Apellido = c.Apellido,
                              Nombre = c.Nombre,
                              NroDocumento = c.NroDocumento,
                              TipoDocumentoDescripcion = c.TipoDocumento.Descripcion,
                              TipoDocumentoId = c.TipoDocumentoId
                          });
            return result.FirstOrDefault();
        }

        public Cliente GetClienteById(int clienteId)
        {
            var activeContext = UnitOfWork as IMainModuleUnitOfWork;
            if (activeContext == null)
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Resources.Messages.InvalidContext", this.GetType().Name));

            return activeContext.Cliente.SingleOrDefault(x => x.ClienteId == clienteId);
        }
    }
}
